Source for file class.ezpdf.php

Documentation is available at class.ezpdf.php

  1. <?php
  2.  
  3. include_once('includes/class.pdf.php');
  4.  
  5. class Cezpdf extends Cpdf {
  6. //==============================================================================
  7. // this class will take the basic interaction facilities of the Cpdf class
  8. // and make more useful functions so that the user does not have to
  9. // know all the ins and outs of pdf presentation to produce something pretty.
  10. //
  11. // IMPORTANT NOTE
  12. // there is no warranty, implied or otherwise with this software.
  13. //
  14. // version 009 (versioning is linked to class.pdf.php)
  15. //
  16. // released under a public domain licence.
  17. //
  18. // Wayne Munro, R&OS Ltd, http://www.ros.co.nz/pdf
  19. //==============================================================================
  20.  
  21. var $ez=array('fontSize'=>10)// used for storing most of the page configuration parameters
  22. var $y// this is the current vertical positon on the page of the writing point, very important
  23. var $ezPages=array()// keep an array of the ids of the pages, making it easy to go back and add page numbers etc.
  24. var $ezPageCount=0;
  25.  
  26. // ------------------------------------------------------------------------------
  27.  
  28. function Cezpdf($paper='a4',$orientation='portrait'){
  29.     // Assuming that people don't want to specify the paper size using the absolute coordinates
  30.     // allow a couple of options:
  31.     // orientation can be 'portrait' or 'landscape'
  32.     // or, to actually set the coordinates, then pass an array in as the first parameter.
  33.     // the defaults are as shown.
  34.     //
  35.     // -------------------------
  36.     // 2002-07-24 - Nicola Asuni (info@tecnick.com):
  37.     // Added new page formats (45 standard ISO paper formats and 4 american common formats)
  38.     // paper cordinates are calculated in this way: (inches * 72) where 1 inch = 2.54 cm
  39.     //
  40.     // Now you may also pass a 2 values array containing the page width and height in centimeters
  41.     // -------------------------
  42.  
  43.     if (!is_array($paper)){
  44.         switch (strtoupper($paper)){
  45.             case '4A0'{$size array(0,0,4767.87,6740.79)break;}
  46.             case '2A0'{$size array(0,0,3370.39,4767.87)break;}
  47.             case 'A0'{$size array(0,0,2383.94,3370.39)break;}
  48.             case 'A1'{$size array(0,0,1683.78,2383.94)break;}
  49.             case 'A2'{$size array(0,0,1190.55,1683.78)break;}
  50.             case 'A3'{$size array(0,0,841.89,1190.55)break;}
  51.             case 'A4'default{$size array(0,0,595.28,841.89)break;}
  52.             case 'A5'{$size array(0,0,419.53,595.28)break;}
  53.             case 'A6'{$size array(0,0,297.64,419.53)break;}
  54.             case 'A7'{$size array(0,0,209.76,297.64)break;}
  55.             case 'A8'{$size array(0,0,147.40,209.76)break;}
  56.             case 'A9'{$size array(0,0,104.88,147.40)break;}
  57.             case 'A10'{$size array(0,0,73.70,104.88)break;}
  58.             case 'B0'{$size array(0,0,2834.65,4008.19)break;}
  59.             case 'B1'{$size array(0,0,2004.09,2834.65)break;}
  60.             case 'B2'{$size array(0,0,1417.32,2004.09)break;}
  61.             case 'B3'{$size array(0,0,1000.63,1417.32)break;}
  62.             case 'B4'{$size array(0,0,708.66,1000.63)break;}
  63.             case 'B5'{$size array(0,0,498.90,708.66)break;}
  64.             case 'B6'{$size array(0,0,354.33,498.90)break;}
  65.             case 'B7'{$size array(0,0,249.45,354.33)break;}
  66.             case 'B8'{$size array(0,0,175.75,249.45)break;}
  67.             case 'B9'{$size array(0,0,124.72,175.75)break;}
  68.             case 'B10'{$size array(0,0,87.87,124.72)break;}
  69.             case 'C0'{$size array(0,0,2599.37,3676.54)break;}
  70.             case 'C1'{$size array(0,0,1836.85,2599.37)break;}
  71.             case 'C2'{$size array(0,0,1298.27,1836.85)break;}
  72.             case 'C3'{$size array(0,0,918.43,1298.27)break;}
  73.             case 'C4'{$size array(0,0,649.13,918.43)break;}
  74.             case 'C5'{$size array(0,0,459.21,649.13)break;}
  75.             case 'C6'{$size array(0,0,323.15,459.21)break;}
  76.             case 'C7'{$size array(0,0,229.61,323.15)break;}
  77.             case 'C8'{$size array(0,0,161.57,229.61)break;}
  78.             case 'C9'{$size array(0,0,113.39,161.57)break;}
  79.             case 'C10'{$size array(0,0,79.37,113.39)break;}
  80.             case 'RA0'{$size array(0,0,2437.80,3458.27)break;}
  81.             case 'RA1'{$size array(0,0,1729.13,2437.80)break;}
  82.             case 'RA2'{$size array(0,0,1218.90,1729.13)break;}
  83.             case 'RA3'{$size array(0,0,864.57,1218.90)break;}
  84.             case 'RA4'{$size array(0,0,609.45,864.57)break;}
  85.             case 'SRA0'{$size array(0,0,2551.18,3628.35)break;}
  86.             case 'SRA1'{$size array(0,0,1814.17,2551.18)break;}
  87.             case 'SRA2'{$size array(0,0,1275.59,1814.17)break;}
  88.             case 'SRA3'{$size array(0,0,907.09,1275.59)break;}
  89.             case 'SRA4'{$size array(0,0,637.80,907.09)break;}
  90.             case 'LETTER'{$size array(0,0,612.00,792.00)break;}
  91.             case 'LEGAL'{$size array(0,0,612.00,1008.00)break;}
  92.             case 'EXECUTIVE'{$size array(0,0,521.86,756.00)break;}
  93.             case 'FOLIO'{$size array(0,0,612.00,936.00)break;}
  94.         }
  95.         switch (strtolower($orientation)){
  96.             case 'landscape':
  97.                 $a=$size[3];
  98.                 $size[3]=$size[2];
  99.                 $size[2]=$a;
  100.                 break;
  101.         }
  102.     else {
  103.         if (count($paper)>2{
  104.             // then an array was sent it to set the size
  105.             $size $paper;
  106.         }
  107.         else //size in centimeters has been passed
  108.             $size[00;
  109.             $size[10;
  110.             $size[2$paper[02.54 72;
  111.             $size[3$paper[12.54 72;
  112.         }
  113.     }
  114.     $this->Cpdf($size);
  115.     $this->ez['pageWidth']=$size[2];
  116.     $this->ez['pageHeight']=$size[3];
  117.  
  118.     // also set the margins to some reasonable defaults
  119.     $this->ez['topMargin']=30;
  120.     $this->ez['bottomMargin']=30;
  121.     $this->ez['leftMargin']=30;
  122.     $this->ez['rightMargin']=30;
  123.  
  124.     // set the current writing position to the top of the first page
  125.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  126.     // and get the ID of the page that was created during the instancing process.
  127.     $this->ezPages[1]=$this->getFirstPageId();
  128.     $this->ezPageCount=1;
  129. }
  130.  
  131. // ------------------------------------------------------------------------------
  132. // 2002-07-24: Nicola Asuni (info@tecnick.com)
  133. // Set Margins in centimeters
  134. function ezSetCmMargins($top,$bottom,$left,$right){
  135.     $top $top 2.54 72;
  136.     $bottom $bottom 2.54 72;
  137.     $left $left 2.54 72;
  138.     $right $right 2.54 72;
  139.     $this->ezSetMargins($top,$bottom,$left,$right);
  140. }
  141. // ------------------------------------------------------------------------------
  142.  
  143.  
  144. function ezColumnsStart($options=array()){
  145.   // start from the current y-position, make the set number of columne
  146.   if (isset($this->ez['columns']&& $this->ez['columns']==1){
  147.     // if we are already in a column mode then just return.
  148.     return;
  149.   }
  150.   $def=array('gap'=>10,'num'=>2);
  151.   foreach($def as $k=>$v){
  152.     if (!isset($options[$k])){
  153.       $options[$k]=$v;
  154.     }
  155.   }
  156.   // setup the columns
  157.   $this->ez['columns']=array('on'=>1,'colNum'=>1);
  158.  
  159.   // store the current margins
  160.   $this->ez['columns']['margins']=array(
  161.      $this->ez['leftMargin']
  162.     ,$this->ez['rightMargin']
  163.     ,$this->ez['topMargin']
  164.     ,$this->ez['bottomMargin']
  165.   );
  166.   // and store the settings for the columns
  167.   $this->ez['columns']['options']=$options;
  168.   // then reset the margins to suit the new columns
  169.   // safe enough to assume the first column here, but start from the current y-position
  170.   $this->ez['topMargin']=$this->ez['pageHeight']-$this->y;
  171.   $width=($this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin']-($options['num']-1)*$options['gap'])/$options['num'];
  172.   $this->ez['columns']['width']=$width;
  173.   $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  174.  
  175. }
  176. // ------------------------------------------------------------------------------
  177. function ezColumnsStop(){
  178.   if (isset($this->ez['columns']&& $this->ez['columns']['on']==1){
  179.     $this->ez['columns']['on']=0;
  180.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0];
  181.     $this->ez['rightMargin']=$this->ez['columns']['margins'][1];
  182.     $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  183.     $this->ez['bottomMargin']=$this->ez['columns']['margins'][3];
  184.   }
  185. }
  186. // ------------------------------------------------------------------------------
  187. function ezInsertMode($status=1,$pageNum=1,$pos='before'){
  188.   // puts the document into insert mode. new pages are inserted until this is re-called with status=0
  189.   // by default pages wil be inserted at the start of the document
  190.   switch($status){
  191.     case '1':
  192.       if (isset($this->ezPages[$pageNum])){
  193.         $this->ez['insertMode']=1;
  194.         $this->ez['insertOptions']=array('id'=>$this->ezPages[$pageNum],'pos'=>$pos);
  195.       }
  196.       break;
  197.     case '0':
  198.       $this->ez['insertMode']=0;
  199.       break;
  200.   }
  201. }
  202. // ------------------------------------------------------------------------------
  203.  
  204. function ezNewPage(){
  205.   $pageRequired=1;
  206.   if (isset($this->ez['columns']&& $this->ez['columns']['on']==1){
  207.     // check if this is just going to a new column
  208.     // increment the column number
  209. //echo 'HERE<br>';
  210.     $this->ez['columns']['colNum']++;
  211. //echo $this->ez['columns']['colNum'].'<br>';
  212.     if ($this->ez['columns']['colNum'<= $this->ez['columns']['options']['num']){
  213.       // then just reset to the top of the next column
  214.       $pageRequired=0;
  215.     else {
  216.       $this->ez['columns']['colNum']=1;
  217.       $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  218.     }
  219.  
  220.     $width $this->ez['columns']['width'];
  221.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0]+($this->ez['columns']['colNum']-1)*($this->ez['columns']['options']['gap']+$width);
  222.     $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  223.   }
  224. //echo 'left='.$this->ez['leftMargin'].'   right='.$this->ez['rightMargin'].'<br>';
  225.  
  226.   if ($pageRequired){
  227.     // make a new page, setting the writing point back to the top
  228.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  229.     // make the new page with a call to the basic class.
  230.     $this->ezPageCount++;
  231.     if (isset($this->ez['insertMode']&& $this->ez['insertMode']==1){
  232.       $id $this->ezPages[$this->ezPageCount$this->newPage(1,$this->ez['insertOptions']['id'],$this->ez['insertOptions']['pos']);
  233.       // then manipulate the insert options so that inserted pages follow each other
  234.       $this->ez['insertOptions']['id']=$id;
  235.       $this->ez['insertOptions']['pos']='after';
  236.     else {
  237.       $this->ezPages[$this->ezPageCount$this->newPage();
  238.     }
  239.   else {
  240.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  241.   }
  242. }
  243.  
  244. // ------------------------------------------------------------------------------
  245.  
  246. function ezSetMargins($top,$bottom,$left,$right){
  247.   // sets the margins to new values
  248.   $this->ez['topMargin']=$top;
  249.   $this->ez['bottomMargin']=$bottom;
  250.   $this->ez['leftMargin']=$left;
  251.   $this->ez['rightMargin']=$right;
  252.   // check to see if this means that the current writing position is outside the
  253.   // writable area
  254.   if ($this->y > $this->ez['pageHeight']-$top){
  255.     // then move y down
  256.     $this->y = $this->ez['pageHeight']-$top;
  257.   }
  258.   if $this->y < $bottom){
  259.     // then make a new page
  260.     $this->ezNewPage();
  261.   }
  262. }
  263.  
  264. // ------------------------------------------------------------------------------
  265.  
  266.   // return the strict numbering (1,2,3,4..) number of the current page
  267.   return $this->ezPageCount;
  268. }
  269.  
  270. // ------------------------------------------------------------------------------
  271.  
  272. function ezStartPageNumbers($x,$y,$size,$pos='left',$pattern='{PAGENUM} of {TOTALPAGENUM}',$num=''){
  273.   // put page numbers on the pages from here.
  274.   // place then on the 'pos' side of the coordinates (x,y).
  275.   // pos can be 'left' or 'right'
  276.   // use the given 'pattern' for display, where (PAGENUM} and {TOTALPAGENUM} are replaced
  277.   // as required.
  278.   // if $num is set, then make the first page this number, the number of total pages will
  279.   // be adjusted to account for this.
  280.   // Adjust this function so that each time you 'start' page numbers then you effectively start a different batch
  281.   // return the number of the batch, so that they can be stopped in a different order if required.
  282.   if (!$pos || !strlen($pos)){
  283.     $pos='left';
  284.   }
  285.   if (!$pattern || !strlen($pattern)){
  286.     $pattern='{PAGENUM} of {TOTALPAGENUM}';
  287.   }
  288.   if (!isset($this->ez['pageNumbering'])){
  289.     $this->ez['pageNumbering']=array();
  290.   }
  291.   $i count($this->ez['pageNumbering']);
  292.   $this->ez['pageNumbering'][$i][$this->ezPageCount]=array('x'=>$x,'y'=>$y,'pos'=>$pos,'pattern'=>$pattern,'num'=>$num,'size'=>$size);
  293.   return $i;
  294. }
  295.  
  296. // ------------------------------------------------------------------------------
  297.  
  298. function ezWhatPageNumber($pageNum,$i=0){
  299.   // given a particular generic page number (ie, document numbered sequentially from beginning),
  300.   // return the page number under a particular page numbering scheme ($i)
  301.   $num=0;
  302.   $start=1;
  303.   $startNum=1;
  304.   if (!isset($this->ez['pageNumbering']))
  305.   {
  306.     $this->addMessage('WARNING: page numbering called for and wasn\'t started with ezStartPageNumbers');
  307.     return 0;
  308.   }
  309.   foreach($this->ez['pageNumbering'][$ias $k=>$v){
  310.     if ($k<=$pageNum){
  311.       if (is_array($v)){
  312.         // start block
  313.         if (strlen($v['num'])){
  314.           // a start was specified
  315.           $start=$v['num'];
  316.           $startNum=$k;
  317.           $num=$pageNum-$startNum+$start;
  318.         }
  319.       else {
  320.         // stop block
  321.         $num=0;
  322.       }
  323.     }
  324.   }
  325.   return $num;
  326. }
  327.  
  328. // ------------------------------------------------------------------------------
  329.  
  330. function ezStopPageNumbers($stopTotal=0,$next=0,$i=0){
  331.   // if stopTotal=1 then the totalling of pages for this number will stop too
  332.   // if $next=1, then do this page, but not the next, else do not do this page either
  333.   // if $i is set, then stop that particular pagenumbering sequence.
  334.   if (!isset($this->ez['pageNumbering'])){
  335.     $this->ez['pageNumbering']=array();
  336.   }
  337.   if ($next && isset($this->ez['pageNumbering'][$i][$this->ezPageCount]&& is_array($this->ez['pageNumbering'][$i][$this->ezPageCount])){
  338.     // then this has only just been started, this will over-write the start, and nothing will appear
  339.     // add a special command to the start block, telling it to stop as well
  340.     if ($stopTotal){
  341.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stoptn']=1;
  342.     else {
  343.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stopn']=1;
  344.     }
  345.   else {
  346.     if ($stopTotal){
  347.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stopt';
  348.     else {
  349.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stop';
  350.     }
  351.     if ($next){
  352.       $this->ez['pageNumbering'][$i][$this->ezPageCount].='n';
  353.     }
  354.   }
  355. }
  356.  
  357. // ------------------------------------------------------------------------------
  358.  
  359. function ezPRVTpageNumberSearch($lbl,&$tmp){
  360.   foreach($tmp as $i=>$v){
  361.     if (is_array($v)){
  362.       if (isset($v[$lbl])){
  363.         return $i;
  364.       }
  365.     else {
  366.       if ($v==$lbl){
  367.         return $i;
  368.       }
  369.     }
  370.   }
  371.   return 0;
  372. }
  373.  
  374. // ------------------------------------------------------------------------------
  375.  
  376.   // this will go through the pageNumbering array and add the page numbers are required
  377.   if (isset($this->ez['pageNumbering'])){
  378.     $totalPages1 $this->ezPageCount;
  379.     $tmp1=$this->ez['pageNumbering'];
  380.     $status=0;
  381.     foreach($tmp1 as $i=>$tmp){
  382.       // do each of the page numbering systems
  383.       // firstly, find the total pages for this one
  384.       $k $this->ezPRVTpageNumberSearch('stopt',$tmp);
  385.       if ($k && $k>0){
  386.         $totalPages $k-1;
  387.       else {
  388.         $l $this->ezPRVTpageNumberSearch('stoptn',$tmp);
  389.         if ($l && $l>0){
  390.           $totalPages $l;
  391.         else {
  392.           $totalPages $totalPages1;
  393.         }
  394.       }
  395.       foreach ($this->ezPages as $pageNum=>$id){
  396.         if (isset($tmp[$pageNum])){
  397.           if (is_array($tmp[$pageNum])){
  398.             // then this must be starting page numbers
  399.             $status=1;
  400.             $info $tmp[$pageNum];
  401.             $info['dnum']=$info['num']-$pageNum;
  402.             // also check for the special case of the numbering stopping and starting on the same page
  403.             if (isset($info['stopn']|| isset($info['stoptn']) ){
  404.               $status=2;
  405.             }
  406.           else if ($tmp[$pageNum]=='stop' || $tmp[$pageNum]=='stopt'){
  407.             // then we are stopping page numbers
  408.             $status=0;
  409.           else if ($status==&& ($tmp[$pageNum]=='stoptn' || $tmp[$pageNum]=='stopn')){
  410.             // then we are stopping page numbers
  411.             $status=2;
  412.           }
  413.         }
  414.         if ($status){
  415.           // then add the page numbering to this page
  416.           if (strlen($info['num'])){
  417.             $num=$pageNum+$info['dnum'];
  418.           else {
  419.             $num=$pageNum;
  420.           }
  421.           $total $totalPages+$num-$pageNum;
  422.           $pat str_replace('{PAGENUM}',$num,$info['pattern']);
  423.           $pat str_replace('{TOTALPAGENUM}',$total,$pat);
  424.           $this->reopenObject($id);
  425.           switch($info['pos']){
  426.             case 'right':
  427.               $this->addText($info['x'],$info['y'],$info['size'],$pat);
  428.               break;
  429.             default:
  430.               $w=$this->getTextWidth($info['size'],$pat);
  431.               $this->addText($info['x']-$w,$info['y'],$info['size'],$pat);
  432.               break;
  433.           }
  434.           $this->closeObject();
  435.         }
  436.         if ($status==2){
  437.           $status=0;
  438.         }
  439.       }
  440.     }
  441.   }
  442. }
  443.  
  444. // ------------------------------------------------------------------------------
  445.  
  446. function ezPRVTcleanUp(){
  447.   $this->ezPRVTaddPageNumbers();
  448. }
  449.  
  450. // ------------------------------------------------------------------------------
  451.  
  452. function ezStream($options=''){
  453.   $this->ezPRVTcleanUp();
  454.   $this->stream($options);
  455. }
  456.  
  457. // ------------------------------------------------------------------------------
  458.  
  459. function ezOutput($options=0){
  460.   $this->ezPRVTcleanUp();
  461.   return $this->output($options);
  462. }
  463.  
  464. // ------------------------------------------------------------------------------
  465.  
  466. function ezSetY($y){
  467.   // used to change the vertical position of the writing point.
  468.   $this->y = $y;
  469.   if $this->y < $this->ez['bottomMargin']){
  470.     // then make a new page
  471.     $this->ezNewPage();
  472.   }
  473. }
  474.  
  475. // ------------------------------------------------------------------------------
  476.  
  477. function ezSetDy($dy,$mod=''){
  478.   // used to change the vertical position of the writing point.
  479.   // changes up by a positive increment, so enter a negative number to go
  480.   // down the page
  481.   // if $mod is set to 'makeSpace' and a new page is forced, then the pointed will be moved
  482.   // down on the new page, this will allow space to be reserved for graphics etc.
  483.   $this->y += $dy;
  484.   if $this->y < $this->ez['bottomMargin']){
  485.     // then make a new page
  486.     $this->ezNewPage();
  487.     if ($mod=='makeSpace'){
  488.       $this->y += $dy;
  489.     }
  490.   }
  491. }
  492.  
  493. // ------------------------------------------------------------------------------
  494.  
  495. function ezPrvtTableDrawLines($pos,$gap,$x0,$x1,$y0,$y1,$y2,$col,$inner,$outer,$opt=1){
  496.   $x0=1000;
  497.   $x1=0;
  498.   $this->setStrokeColor($col[0],$col[1],$col[2]);
  499.   $cnt=0;
  500.   $n count($pos);
  501.   foreach($pos as $x){
  502.     $cnt++;
  503.     if ($cnt==|| $cnt==$n){
  504.       $this->setLineStyle($outer);
  505.     else {
  506.       $this->setLineStyle($inner);
  507.     }
  508.     $this->line($x-$gap/2,$y0,$x-$gap/2,$y2);
  509.     if ($x>$x1)$x1=$x};
  510.     if ($x<$x0)$x0=$x};
  511.   }
  512.   $this->setLineStyle($outer);
  513.   $this->line($x0-$gap/2-$outer/2,$y0,$x1-$gap/2+$outer/2,$y0);
  514.   // only do the second line if it is different to the first, AND each row does not have
  515.   // a line on it.
  516.   if ($y0!=$y1 && $opt<2){
  517.     $this->line($x0-$gap/2,$y1,$x1-$gap/2,$y1);
  518.   }
  519.   $this->line($x0-$gap/2-$outer/2,$y2,$x1-$gap/2+$outer/2,$y2);
  520. }
  521.  
  522. // ------------------------------------------------------------------------------
  523.  
  524. function ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$gap,$size,&$y,$optionsAll=array()){
  525.   // uses ezText to add the text, and returns the height taken by the largest heading
  526.   // this page will move the headings to a new page if they will not fit completely on this one
  527.   // transaction support will be used to implement this
  528.  
  529.   if (isset($optionsAll['cols'])){
  530.     $options $optionsAll['cols'];
  531.   else {
  532.     $options array();
  533.   }
  534.  
  535.   $mx=0;
  536.   $startPage $this->ezPageCount;
  537.   $secondGo=0;
  538.  
  539.   // $y is the position at which the top of the table should start, so the base
  540.   // of the first text, is $y-$height-$gap-$decender, but ezText starts by dropping $height
  541.  
  542.   // the return from this function is the total cell height, including gaps, and $y is adjusted
  543.   // to be the postion of the bottom line
  544.  
  545.   // begin the transaction
  546.   $this->transaction('start');
  547.   $ok=0;
  548. //  $y-=$gap-$decender;
  549.   $y-=$gap;
  550.   while ($ok==0){
  551.     foreach($cols as $colName=>$colHeading){
  552.       $this->ezSetY($y);
  553.       if (isset($options[$colName]&& isset($options[$colName]['justification'])){
  554.         $justification $options[$colName]['justification'];
  555.       else {
  556.         $justification 'left';
  557.       }
  558.       $this->ezText($colHeading,$size,array('aleft'=> $pos[$colName],'aright'=>($maxWidth[$colName]+$pos[$colName]),'justification'=>$justification));
  559.       $dy $y-$this->y;
  560.       if ($dy>$mx){
  561.         $mx=$dy;
  562.       }
  563.     }
  564.     $y $y $mx $gap $decender;
  565. //    $y -= $mx-$gap+$decender;
  566.  
  567.     // now, if this has moved to a new page, then abort the transaction, move to a new page, and put it there
  568.     // do not check on the second time around, to avoid an infinite loop
  569.     if ($this->ezPageCount != $startPage && $secondGo==0){
  570.       $this->transaction('rewind');
  571.       $this->ezNewPage();
  572.       $y $this->y - $gap-$decender;
  573.       $ok=0;
  574.       $secondGo=1;
  575. //      $y = $store_y;
  576.       $mx=0;
  577.  
  578.     else {
  579.       $this->transaction('commit');
  580.       $ok=1;
  581.     }
  582.   }
  583.  
  584.   return $mx+$gap*2-$decender;
  585. }
  586.  
  587. // ------------------------------------------------------------------------------
  588.  
  589. function ezPrvtGetTextWidth($size,$text){
  590.   // will calculate the maximum width, taking into account that the text may be broken
  591.   // by line breaks.
  592.   $mx=0;
  593.   $lines explode("\n",$text);
  594.   foreach ($lines as $line){
  595.     $w $this->getTextWidth($size,$line);
  596.     if ($w>$mx){
  597.       $mx=$w;
  598.     }
  599.   }
  600.   return $mx;
  601. }
  602.  
  603. // ------------------------------------------------------------------------------
  604.  
  605. function ezTable(&$data,$cols='',$title='',$options=''){
  606.   // add a table of information to the pdf document
  607.   // $data is a two dimensional array
  608.   // $cols (optional) is an associative array, the keys are the names of the columns from $data
  609.   // to be presented (and in that order), the values are the titles to be given to the columns
  610.   // $title (optional) is the title to be put on the top of the table
  611.   //
  612.   // $options is an associative array which can contain:
  613.   // 'showLines'=> 0,1,2, default is 1 (show outside and top lines only), 2=> lines on each row
  614.   // 'showHeadings' => 0 or 1
  615.   // 'shaded'=> 0,1,2,3 default is 1 (1->alternate lines are shaded, 0->no shading, 2-> both shaded, second uses shadeCol2)
  616.   // 'shadeCol' => (r,g,b) array, defining the colour of the shading, default is (0.8,0.8,0.8)
  617.   // 'shadeCol2' => (r,g,b) array, defining the colour of the shading of the other blocks, default is (0.7,0.7,0.7)
  618.   // 'fontSize' => 10
  619.   // 'textCol' => (r,g,b) array, text colour
  620.   // 'titleFontSize' => 12
  621.   // 'rowGap' => 2 , the space added at the top and bottom of each row, between the text and the lines
  622.   // 'colGap' => 5 , the space on the left and right sides of each cell
  623.   // 'lineCol' => (r,g,b) array, defining the colour of the lines, default, black.
  624.   // 'xPos' => 'left','right','center','centre',or coordinate, reference coordinate in the x-direction
  625.   // 'xOrientation' => 'left','right','center','centre', position of the table w.r.t 'xPos'
  626.   // 'width'=> <number> which will specify the width of the table, if it turns out to not be this
  627.   //    wide, then it will stretch the table to fit, if it is wider then each cell will be made
  628.   //    proportionalty smaller, and the content may have to wrap.
  629.   // 'maxWidth'=> <number> similar to 'width', but will only make table smaller than it wants to be
  630.   // 'options' => array(<colname>=>array('justification'=>'left','width'=>100,'link'=>linkDataName),<colname>=>....)
  631.   //             allow the setting of other paramaters for the individual columns
  632.   // 'minRowSpace'=> the minimum space between the bottom of each row and the bottom margin, in which a new row will be started
  633.   //                  if it is less, then a new page would be started, default=-100
  634.   // 'innerLineThickness'=>1
  635.   // 'outerLineThickness'=>1
  636.   // 'splitRows'=>0, 0 or 1, whether or not to allow the rows to be split across page boundaries
  637.   // 'protectRows'=>number, the number of rows to hold with the heading on page, ie, if there less than this number of
  638.   //                rows on the page, then move the whole lot onto the next page, default=1
  639.   //
  640.   // note that the user will have had to make a font selection already or this will not
  641.   // produce a valid pdf file.
  642.  
  643.   if (!is_array($data)){
  644.     return;
  645.   }
  646.  
  647.   if (!is_array($cols)){
  648.     // take the columns from the first row of the data set
  649.     reset($data);
  650.     list($k,$v)=each($data);
  651.     if (!is_array($v)){
  652.       return;
  653.     }
  654.     $cols=array();
  655.     foreach($v as $k1=>$v1){
  656.       $cols[$k1]=$k1;
  657.     }
  658.   }
  659.  
  660.   if (!is_array($options)){
  661.     $options=array();
  662.   }
  663.  
  664.   $defaults array(
  665.     'shaded'=>1,'showLines'=>1,'shadeCol'=>array(0.8,0.8,0.8),'shadeCol2'=>array(0.7,0.7,0.7),'fontSize'=>10,'titleFontSize'=>12
  666.     ,'titleGap'=>5,'lineCol'=>array(0,0,0),'gap'=>5,'xPos'=>'centre','xOrientation'=>'centre'
  667.     ,'showHeadings'=>1,'textCol'=>array(0,0,0),'width'=>0,'maxWidth'=>0,'cols'=>array(),'minRowSpace'=>-100,'rowGap'=>2,'colGap'=>5
  668.     ,'innerLineThickness'=>1,'outerLineThickness'=>1,'splitRows'=>0,'protectRows'=>1
  669.     );
  670.  
  671.   foreach($defaults as $key=>$value){
  672.     if (is_array($value)){
  673.       if (!isset($options[$key]|| !is_array($options[$key])){
  674.         $options[$key]=$value;
  675.       }
  676.     else {
  677.       if (!isset($options[$key])){
  678.         $options[$key]=$value;
  679.       }
  680.     }
  681.   }
  682.   $options['gap']=2*$options['colGap'];
  683.  
  684.   $middle ($this->ez['pageWidth']-$this->ez['rightMargin'])/2+($this->ez['leftMargin'])/2;
  685.   // figure out the maximum widths of the text within each column
  686.   $maxWidth=array();
  687.   foreach($cols as $colName=>$colHeading){
  688.     $maxWidth[$colName]=0;
  689.   }
  690.   // find the maximum cell widths based on the data
  691.   foreach($data as $row){
  692.     foreach($cols as $colName=>$colHeading){
  693.       $w $this->ezPrvtGetTextWidth($options['fontSize'],(string)$row[$colName])*1.01;
  694.       if ($w $maxWidth[$colName]){
  695.         $maxWidth[$colName]=$w;
  696.       }
  697.     }
  698.   }
  699.   // and the maximum widths to fit in the headings
  700.   foreach($cols as $colName=>$colTitle){
  701.     $w $this->ezPrvtGetTextWidth($options['fontSize'],(string)$colTitle)*1.01;
  702.     if ($w $maxWidth[$colName]){
  703.       $maxWidth[$colName]=$w;
  704.     }
  705.   }
  706.  
  707.   // calculate the start positions of each of the columns
  708.   $pos=array();
  709.   $x=0;
  710.   $t=$x;
  711.   $adjustmentWidth=0;
  712.   $setWidth=0;
  713.   foreach($maxWidth as $colName => $w){
  714.     $pos[$colName]=$t;
  715.     // if the column width has been specified then set that here, also total the
  716.     // width avaliable for adjustment
  717.     if (isset($options['cols'][$colName]&& isset($options['cols'][$colName]['width']&& $options['cols'][$colName]['width']>0){
  718.       $t=$t+$options['cols'][$colName]['width'];
  719.       $maxWidth[$colName$options['cols'][$colName]['width']-$options['gap'];
  720.       $setWidth += $options['cols'][$colName]['width'];
  721.     else {
  722.       $t=$t+$w+$options['gap'];
  723.       $adjustmentWidth += $w;
  724.       $setWidth += $options['gap'];
  725.     }
  726.   }
  727.   $pos['_end_']=$t;
  728.  
  729.   // if maxWidth is specified, and the table is too wide, and the width has not been set,
  730.   // then set the width.
  731.   if ($options['width']==&& $options['maxWidth'&& ($t-$x)>$options['maxWidth']){
  732.     // then need to make this one smaller
  733.     $options['width']=$options['maxWidth'];
  734.   }
  735.  
  736.   if ($options['width'&& $adjustmentWidth>&& $setWidth<$options['width']){
  737.     // first find the current widths of the columns involved in this mystery
  738.     $cols0 array();
  739.     $cols1 array();
  740.     $xq=0;
  741.     $presentWidth=0;
  742.     $last='';
  743.     foreach($pos as $colName=>$p){
  744.       if (!isset($options['cols'][$last]|| !isset($options['cols'][$last]['width']|| $options['cols'][$last]['width']<=0){
  745.         if (strlen($last)){
  746.           $cols0[$last]=$p-$xq -$options['gap'];
  747.           $presentWidth += ($p-$xq $options['gap']);
  748.         }
  749.       else {
  750.         $cols1[$last]=$p-$xq;
  751.       }
  752.       $last=$colName;
  753.       $xq=$p;
  754.     }
  755.     // $cols0 contains the widths of all the columns which are not set
  756.     $neededWidth $options['width']-$setWidth;
  757.     // if needed width is negative then add it equally to each column, else get more tricky
  758.     if ($presentWidth<$neededWidth){
  759.       foreach($cols0 as $colName=>$w){
  760.         $cols0[$colName]+= ($neededWidth-$presentWidth)/count($cols0);
  761.       }
  762.     else {
  763.  
  764.       $cnt=0;
  765.       while ($presentWidth>$neededWidth && $cnt<100){
  766.         $cnt++// insurance policy
  767.         // find the widest columns, and the next to widest width
  768.         $aWidest array();
  769.         $nWidest=0;
  770.         $widest=0;
  771.         foreach($cols0 as $colName=>$w){
  772.           if ($w>$widest){
  773.             $aWidest=array($colName);
  774.             $nWidest $widest;
  775.             $widest=$w;
  776.           else if ($w==$widest){
  777.             $aWidest[]=$colName;
  778.           }
  779.         }
  780.         // then figure out what the width of the widest columns would have to be to take up all the slack
  781.         $newWidestWidth $widest ($presentWidth-$neededWidth)/count($aWidest);
  782.         if ($newWidestWidth $nWidest){
  783.           // then there is space to set them to this
  784.           foreach($aWidest as $colName){
  785.             $cols0[$colName$newWidestWidth;
  786.           }
  787.           $presentWidth=$neededWidth;
  788.         else {
  789.           // there is not space, reduce the size of the widest ones down to the next size down, and we
  790.           // will go round again
  791.           foreach($aWidest as $colName){
  792.             $cols0[$colName$nWidest;
  793.           }
  794.           $presentWidth=$presentWidth-($widest-$nWidest)*count($aWidest);
  795.         }
  796.       }
  797.     }
  798.     // $cols0 now contains the new widths of the constrained columns.
  799.     // now need to update the $pos and $maxWidth arrays
  800.     $xq=0;
  801.     foreach($pos as $colName=>$p){
  802.       $pos[$colName]=$xq;
  803.       if (!isset($options['cols'][$colName]|| !isset($options['cols'][$colName]['width']|| $options['cols'][$colName]['width']<=0){
  804.         if (isset($cols0[$colName])){
  805.           $xq += $cols0[$colName$options['gap'];
  806.           $maxWidth[$colName]=$cols0[$colName];
  807.         }
  808.       else {
  809.         if (isset($cols1[$colName])){
  810.           $xq += $cols1[$colName];
  811.         }
  812.       }
  813.     }
  814.  
  815.     $t=$x+$options['width'];
  816.     $pos['_end_']=$t;
  817.   }
  818.  
  819.   // now adjust the table to the correct location across the page
  820.   switch ($options['xPos']){
  821.     case 'left':
  822.       $xref $this->ez['leftMargin'];
  823.       break;
  824.     case 'right':
  825.       $xref $this->ez['pageWidth'$this->ez['rightMargin'];
  826.       break;
  827.     case 'centre':
  828.     case 'center':
  829.       $xref $middle;
  830.       break;
  831.     default:
  832.       $xref $options['xPos'];
  833.       break;
  834.   }
  835.   switch ($options['xOrientation']){
  836.     case 'left':
  837.       $dx $xref-$t;
  838.       break;
  839.     case 'right':
  840.       $dx $xref;
  841.       break;
  842.     case 'centre':
  843.     case 'center':
  844.       $dx $xref-$t/2;
  845.       break;
  846.   }
  847.  
  848.  
  849.   foreach($pos as $k=>$v){
  850.     $pos[$k]=$v+$dx;
  851.   }
  852.   $x0=$x+$dx;
  853.   $x1=$t+$dx;
  854.  
  855.   $baseLeftMargin $this->ez['leftMargin'];
  856.   $basePos $pos;
  857.   $baseX0 $x0;
  858.   $baseX1 $x1;
  859.  
  860.   // ok, just about ready to make me a table
  861.   $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2]);
  862.   $this->setStrokeColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2]);
  863.  
  864.   $middle ($x1+$x0)/2;
  865.  
  866.   // start a transaction which will be used to regress the table, if there are not enough rows protected
  867.   if ($options['protectRows']>0){
  868.     $this->transaction('start');
  869.     $movedOnce=0;
  870.   }
  871.   $abortTable 1;
  872.   while ($abortTable){
  873.   $abortTable=0;
  874.  
  875.   $dm $this->ez['leftMargin']-$baseLeftMargin;
  876.   foreach($basePos as $k=>$v){
  877.     $pos[$k]=$v+$dm;
  878.   }
  879.   $x0=$baseX0+$dm;
  880.   $x1=$baseX1+$dm;
  881.   $middle ($x1+$x0)/2;
  882.  
  883.  
  884.   // if the title is set, then do that
  885.   if (strlen($title)){
  886.     $w $this->getTextWidth($options['titleFontSize'],$title);
  887.     $this->y -= $this->getFontHeight($options['titleFontSize']);
  888.     if ($this->y < $this->ez['bottomMargin']){
  889.       $this->ezNewPage();
  890.         // margins may have changed on the newpage
  891.         $dm $this->ez['leftMargin']-$baseLeftMargin;
  892.         foreach($basePos as $k=>$v){
  893.           $pos[$k]=$v+$dm;
  894.         }
  895.         $x0=$baseX0+$dm;
  896.         $x1=$baseX1+$dm;
  897.         $middle ($x1+$x0)/2;
  898.       $this->y -= $this->getFontHeight($options['titleFontSize']);
  899.     }
  900.     $this->addText($middle-$w/2,$this->y,$options['titleFontSize'],$title);
  901.     $this->y -= $options['titleGap'];
  902.   }
  903.  
  904.         // margins may have changed on the newpage
  905.         $dm $this->ez['leftMargin']-$baseLeftMargin;
  906.         foreach($basePos as $k=>$v){
  907.           $pos[$k]=$v+$dm;
  908.         }
  909.         $x0=$baseX0+$dm;
  910.         $x1=$baseX1+$dm;
  911.  
  912.   $y=$this->y// to simplify the code a bit
  913.  
  914.   // make the table
  915.   $height $this->getFontHeight($options['fontSize']);
  916.   $decender $this->getFontDecender($options['fontSize']);
  917.  
  918.  
  919.  
  920.   $y0=$y+$decender;
  921.   $dy=0;
  922.   if ($options['showHeadings']){
  923.     // this function will move the start of the table to a new page if it does not fit on this one
  924.     $headingHeight $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  925.     $y0 $y+$headingHeight;
  926.     $y1 $y;
  927.  
  928.  
  929.     $dm $this->ez['leftMargin']-$baseLeftMargin;
  930.     foreach($basePos as $k=>$v){
  931.       $pos[$k]=$v+$dm;
  932.     }
  933.     $x0=$baseX0+$dm;
  934.     $x1=$baseX1+$dm;
  935.  
  936.   else {
  937.     $y1 $y0;
  938.   }
  939.   $firstLine=1;
  940.  
  941.  
  942.   // open an object here so that the text can be put in over the shading
  943.   if ($options['shaded']){
  944.     $this->saveState();
  945.     $textObjectId $this->openObject();
  946.     $this->closeObject();
  947.     $this->addObject($textObjectId);
  948.     $this->reopenObject($textObjectId);
  949.   }
  950.  
  951.   $cnt=0;
  952.   $newPage=0;
  953.   foreach($data as $row){
  954.     $cnt++;
  955.     // the transaction support will be used to prevent rows being split
  956.     if ($options['splitRows']==0){
  957.       $pageStart $this->ezPageCount;
  958.       if (isset($this->ez['columns']&& $this->ez['columns']['on']==1){
  959.         $columnStart $this->ez['columns']['colNum'];
  960.       }
  961.       $this->transaction('start');
  962.       $row_orig $row;
  963.       $y_orig $y;
  964.       $y0_orig $y0;
  965.       $y1_orig $y1;
  966.     }
  967.     $ok=0;
  968.     $secondTurn=0;
  969.     while(!$abortTable && $ok == 0){
  970.  
  971.     $mx=0;
  972.     $newRow=1;
  973.     while(!$abortTable && ($newPage || $newRow)){
  974.  
  975.       $y-=$height;
  976.       if ($newPage || $y<$this->ez['bottomMargin'|| (isset($options['minRowSpace']&& $y<($this->ez['bottomMargin']+$options['minRowSpace'])) ){
  977.         // check that enough rows are with the heading
  978.         if ($options['protectRows']>&& $movedOnce==&& $cnt<=$options['protectRows']){
  979.           // then we need to move the whole table onto the next page
  980.           $movedOnce 1;
  981.           $abortTable 1;
  982.         }
  983.  
  984.         $y2=$y-$mx+2*$height+$decender-$newRow*$height;
  985.         if ($options['showLines']){
  986.           if (!$options['showHeadings']){
  987.             $y0=$y1;
  988.           }
  989.           $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  990.         }
  991.         if ($options['shaded']){
  992.           $this->closeObject();
  993.           $this->restoreState();
  994.         }
  995.         $this->ezNewPage();
  996.         // and the margins may have changed, this is due to the possibility of the columns being turned on
  997.         // as the columns are managed by manipulating the margins
  998.  
  999.         $dm $this->ez['leftMargin']-$baseLeftMargin;
  1000.         foreach($basePos as $k=>$v){
  1001.           $pos[$k]=$v+$dm;
  1002.         }
  1003. //        $x0=$x0+$dm;
  1004. //        $x1=$x1+$dm;
  1005.         $x0=$baseX0+$dm;
  1006.         $x1=$baseX1+$dm;
  1007.  
  1008.         if ($options['shaded']){
  1009.           $this->saveState();
  1010.           $textObjectId $this->openObject();
  1011.           $this->closeObject();
  1012.           $this->addObject($textObjectId);
  1013.           $this->reopenObject($textObjectId);
  1014.         }
  1015.         $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
  1016.         $y $this->ez['pageHeight']-$this->ez['topMargin'];
  1017.         $y0=$y+$decender;
  1018.         $mx=0;
  1019.         if ($options['showHeadings']){
  1020.           $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  1021.           $y1=$y;
  1022.         else {
  1023.           $y1=$y0;
  1024.         }
  1025.         $firstLine=1;
  1026.         $y -= $height;
  1027.       }
  1028.       $newRow=0;
  1029.       // write the actual data
  1030.       // if these cells need to be split over a page, then $newPage will be set, and the remaining
  1031.       // text will be placed in $leftOvers
  1032.       $newPage=0;
  1033.       $leftOvers=array();
  1034.  
  1035.       foreach($cols as $colName=>$colTitle){
  1036.         $this->ezSetY($y+$height);
  1037.         $colNewPage=0;
  1038.         if (isset($row[$colName])){
  1039.           if (isset($options['cols'][$colName]&& isset($options['cols'][$colName]['link']&& strlen($options['cols'][$colName]['link'])){
  1040.  
  1041.             $lines explode("\n",$row[$colName]);
  1042.             if (isset($row[$options['cols'][$colName]['link']]&& strlen($row[$options['cols'][$colName]['link']])){
  1043.               foreach($lines as $k=>$v){
  1044.                 $lines[$k]='<c:alink:'.$row[$options['cols'][$colName]['link']].'>'.$v.'</c:alink>';
  1045.               }
  1046.             }
  1047.           else {
  1048.             $lines explode("\n",$row[$colName]);
  1049.           }
  1050.         else {
  1051.           $lines array();
  1052.         }
  1053.         $this->y -= $options['rowGap'];
  1054.         foreach ($lines as $line){
  1055.           $line $this->ezProcessText($line);
  1056.           $start=1;
  1057.  
  1058.           while (strlen($line|| $start){
  1059.             $start=0;
  1060.             if (!$colNewPage){
  1061.               $this->y=$this->y-$height;
  1062.             }
  1063.             if ($this->y < $this->ez['bottomMargin']){
  1064.   //            $this->ezNewPage();
  1065.               $newPage=1;  // whether a new page is required for any of the columns
  1066.               $colNewPage=1// whether a new page is required for this column
  1067.             }
  1068.             if ($colNewPage){
  1069.               if (isset($leftOvers[$colName])){
  1070.                 $leftOvers[$colName].="\n".$line;
  1071.               else {
  1072.                 $leftOvers[$colName$line;
  1073.               }
  1074.               $line='';
  1075.             else {
  1076.               if (isset($options['cols'][$colName]&& isset($options['cols'][$colName]['justification']) ){
  1077.                 $just $options['cols'][$colName]['justification'];
  1078.               else {
  1079.                 $just='left';
  1080.               }
  1081.  
  1082.               $line=$this->addTextWrap($pos[$colName],$this->y,$maxWidth[$colName],$options['fontSize'],$line,$just);
  1083.             }
  1084.           }
  1085.         }
  1086.  
  1087.         $dy=$y+$height-$this->y+$options['rowGap'];
  1088.         if ($dy-$height*$newPage>$mx){
  1089.           $mx=$dy-$height*$newPage;
  1090.         }
  1091.       }
  1092.       // set $row to $leftOvers so that they will be processed onto the new page
  1093.       $row $leftOvers;
  1094.       // now add the shading underneath
  1095.       if ($options['shaded'&& $cnt%2==0){
  1096.         $this->closeObject();
  1097.         $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
  1098.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1099.         $this->reopenObject($textObjectId);
  1100.       }
  1101.  
  1102.       if ($options['shaded']==&& $cnt%2==1){
  1103.         $this->closeObject();
  1104.         $this->setColor($options['shadeCol2'][0],$options['shadeCol2'][1],$options['shadeCol2'][2],1);
  1105.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1106.         $this->reopenObject($textObjectId);
  1107.       }
  1108.  
  1109.       if ($options['showLines']>1){
  1110.         // then draw a line on the top of each block
  1111. //        $this->closeObject();
  1112.         $this->saveState();
  1113.         $this->setStrokeColor($options['lineCol'][0],$options['lineCol'][1],$options['lineCol'][2],1);
  1114. //        $this->line($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1115.         if ($firstLine){
  1116.           $this->setLineStyle($options['outerLineThickness']);
  1117.           $firstLine=0;
  1118.         else {
  1119.           $this->setLineStyle($options['innerLineThickness']);
  1120.         }
  1121.         $this->line($x0-$options['gap']/2,$y+$decender+$height,$x1-$options['gap']/2,$y+$decender+$height);
  1122.         $this->restoreState();
  1123. //        $this->reopenObject($textObjectId);
  1124.       }
  1125.     // end of while
  1126.     $y=$y-$mx+$height;
  1127.  
  1128.     // checking row split over pages
  1129.     if ($options['splitRows']==0){
  1130.       if ( ( ($this->ezPageCount != $pageStart|| (isset($this->ez['columns']&& $this->ez['columns']['on']==&& $columnStart != $this->ez['columns']['colNum'))  && $secondTurn==0){
  1131.         // then we need to go back and try that again !
  1132.         $newPage=1;
  1133.         $secondTurn=1;
  1134.         $this->transaction('rewind');
  1135.         $row $row_orig;
  1136.         $y $y_orig;
  1137.         $y0 $y0_orig;
  1138.         $y1 $y1_orig;
  1139.         $ok=0;
  1140.  
  1141.         $dm $this->ez['leftMargin']-$baseLeftMargin;
  1142.         foreach($basePos as $k=>$v){
  1143.           $pos[$k]=$v+$dm;
  1144.         }
  1145.         $x0=$baseX0+$dm;
  1146.         $x1=$baseX1+$dm;
  1147.  
  1148.       else {
  1149.         $this->transaction('commit');
  1150.         $ok=1;
  1151.       }
  1152.     else {
  1153.       $ok=1;  // don't go round the loop if splitting rows is allowed
  1154.     }
  1155.  
  1156.     }  // end of while to check for row splitting
  1157.     if ($abortTable){
  1158.       if ($ok==0){
  1159.         $this->transaction('abort');
  1160.       }
  1161.       // only the outer transaction should be operational
  1162.       $this->transaction('rewind');
  1163.       $this->ezNewPage();
  1164.       break;
  1165.     }
  1166.  
  1167.   // end of foreach ($data as $row)
  1168.  
  1169.   // end of while ($abortTable)
  1170.  
  1171.   // table has been put on the page, the rows guarded as required, commit.
  1172.   $this->transaction('commit');
  1173.  
  1174.   $y2=$y+$decender;
  1175.   if ($options['showLines']){
  1176.     if (!$options['showHeadings']){
  1177.       $y0=$y1;
  1178.     }
  1179.     $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  1180.   }
  1181.  
  1182.   // close the object for drawing the text on top
  1183.   if ($options['shaded']){
  1184.     $this->closeObject();
  1185.     $this->restoreState();
  1186.   }
  1187.  
  1188.   $this->y=$y;
  1189.   return $y;
  1190. }
  1191.  
  1192. // ------------------------------------------------------------------------------
  1193. function ezProcessText($text){
  1194.   // this function will intially be used to implement underlining support, but could be used for a range of other
  1195.   // purposes
  1196.   $search array('<u>','<U>','</u>','</U>');
  1197.   $replace array('<c:uline>','<c:uline>','</c:uline>','</c:uline>');
  1198.   return str_replace($search,$replace,$text);
  1199. }
  1200.  
  1201. // ------------------------------------------------------------------------------
  1202.  
  1203. function ezText($text,$size=0,$options=array(),$test=0){
  1204.   // this will add a string of text to the document, starting at the current drawing
  1205.   // position.
  1206.   // it will wrap to keep within the margins, including optional offsets from the left
  1207.   // and the right, if $size is not specified, then it will be the last one used, or
  1208.   // the default value (12 I think).
  1209.   // the text will go to the start of the next line when a return code "\n" is found.
  1210.   // possible options are:
  1211.   // 'left'=> number, gap to leave from the left margin
  1212.   // 'right'=> number, gap to leave from the right margin
  1213.   // 'aleft'=> number, absolute left position (overrides 'left')
  1214.   // 'aright'=> number, absolute right position (overrides 'right')
  1215.   // 'justification' => 'left','right','center','centre','full'
  1216.  
  1217.   // only set one of the next two items (leading overrides spacing)
  1218.   // 'leading' => number, defines the total height taken by the line, independent of the font height.
  1219.   // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
  1220.  
  1221.   // if $test is set then this should just check if the text is going to flow onto a new page or not, returning true or false
  1222.  
  1223.   // apply the filtering which will make the underlining function.
  1224.   $text $this->ezProcessText($text);
  1225.  
  1226.   $newPage=false;
  1227.   $store_y $this->y;
  1228.  
  1229.   if (is_array($options&& isset($options['aleft'])){
  1230.     $left=$options['aleft'];
  1231.   else {
  1232.     $left $this->ez['leftMargin'((is_array($options&& isset($options['left']))?$options['left']:0);
  1233.   }
  1234.   if (is_array($options&& isset($options['aright'])){
  1235.     $right=$options['aright'];
  1236.   else {
  1237.     $right $this->ez['pageWidth'$this->ez['rightMargin'((is_array($options&& isset($options['right']))?$options['right']:0);
  1238.   }
  1239.   if ($size<=0){
  1240.     $size $this->ez['fontSize'];
  1241.   else {
  1242.     $this->ez['fontSize']=$size;
  1243.   }
  1244.  
  1245.   if (is_array($options&& isset($options['justification'])){
  1246.     $just $options['justification'];
  1247.   else {
  1248.     $just 'left';
  1249.   }
  1250.  
  1251.   // modifications to give leading and spacing based on those given by Craig Heydenburg 1/1/02
  1252.   if (is_array($options&& isset($options['leading'])) ## use leading instead of spacing
  1253.     $height $options['leading'];
  1254.     else if (is_array($options&& isset($options['spacing'])) {
  1255.     $height $this->getFontHeight($size$options['spacing'];
  1256.     else {
  1257.         $height $this->getFontHeight($size);
  1258.     }
  1259.  
  1260.  
  1261.   $lines explode("\n",$text);
  1262.   foreach ($lines as $line){
  1263.     $start=1;
  1264.     while (strlen($line|| $start){
  1265.       $start=0;
  1266.       $this->y=$this->y-$height;
  1267.       if ($this->y < $this->ez['bottomMargin']){
  1268.         if ($test){
  1269.           $newPage=true;
  1270.         else {
  1271.           $this->ezNewPage();
  1272.           // and then re-calc the left and right, in case they have changed due to columns
  1273.         }
  1274.       }
  1275.       if (is_array($options&& isset($options['aleft'])){
  1276.         $left=$options['aleft'];
  1277.       else {
  1278.         $left $this->ez['leftMargin'((is_array($options&& isset($options['left']))?$options['left']:0);
  1279.       }
  1280.       if (is_array($options&& isset($options['aright'])){
  1281.         $right=$options['aright'];
  1282.       else {
  1283.         $right $this->ez['pageWidth'$this->ez['rightMargin'((is_array($options&& isset($options['right']))?$options['right']:0);
  1284.       }
  1285.       $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
  1286.     }
  1287.   }
  1288.  
  1289.   if ($test){
  1290.     $this->y=$store_y;
  1291.     return $newPage;
  1292.   else {
  1293.     return $this->y;
  1294.   }
  1295. }
  1296.  
  1297. // ------------------------------------------------------------------------------
  1298.  
  1299. function ezImage($image,$pad 5,$width 0,$resize 'full',$just 'center',$border ''){
  1300.     //beta ezimage function
  1301.     if (stristr($image,'://'))//copy to temp file
  1302.     {
  1303.         $fp @fopen($image,"rb");
  1304.         while(!feof($fp))
  1305.            {
  1306.                   $cont.= fread($fp,1024);
  1307.            }
  1308.            fclose($fp);
  1309.         $image tempnam ("/tmp""php-pdf");
  1310.         $fp2 @fopen($image,"w");
  1311.            fwrite($fp2,$cont);
  1312.           fclose($fp2);
  1313.         $temp true;
  1314.     }
  1315.  
  1316.     if (!(file_exists($image))) return false//return immediately if image file does not exist
  1317.     $imageInfo getimagesize($image);
  1318.     switch ($imageInfo[2]){
  1319.         case 2:
  1320.             $type "jpeg";
  1321.             break;
  1322.         case 3:
  1323.             $type "png";
  1324.             break;
  1325.         default:
  1326.             return false//return if file is not jpg or png
  1327.     }
  1328.     if ($width == 0$width $imageInfo[0]//set width
  1329.     $ratio $imageInfo[0]/$imageInfo[1];
  1330.  
  1331.     //get maximum width of image
  1332.     if (isset($this->ez['columns']&& $this->ez['columns']['on'== 1)
  1333.     {
  1334.         $bigwidth $this->ez['columns']['width'($pad 2);
  1335.     }
  1336.     else
  1337.     {
  1338.         $bigwidth $this->ez['pageWidth'($pad 2);
  1339.     }
  1340.     //fix width if larger than maximum or if $resize=full
  1341.     if ($resize == 'full' || $resize == 'width' || $width $bigwidth)
  1342.     {
  1343.         $width $bigwidth;
  1344.  
  1345.     }
  1346.  
  1347.     $height ($width/$ratio)//set height
  1348.  
  1349.     //fix size if runs off page
  1350.     if ($height ($this->y - $this->ez['bottomMargin'($pad 2)))
  1351.     {
  1352.         if ($resize != 'full')
  1353.         {
  1354.             $this->ezNewPage();
  1355.         }
  1356.         else
  1357.         {
  1358.             $height ($this->y - $this->ez['bottomMargin'($pad 2))//shrink height
  1359.             $width ($height*$ratio)//fix width
  1360.         }
  1361.     }
  1362.  
  1363.     //fix x-offset if image smaller than bigwidth
  1364.     if ($width $bigwidth)
  1365.     {
  1366.         //center if justification=center
  1367.         if ($just == 'center')
  1368.         {
  1369.             $offset ($bigwidth $width2;
  1370.         }
  1371.         //move to right if justification=right
  1372.         if ($just == 'right')
  1373.         {
  1374.             $offset ($bigwidth $width);
  1375.         }
  1376.         //leave at left if justification=left
  1377.         if ($just == 'left')
  1378.         {
  1379.             $offset 0;
  1380.         }
  1381.     }
  1382.  
  1383.  
  1384.     //call appropriate function
  1385.     if ($type == "jpeg"){
  1386.         $this->addJpegFromFile($image,$this->ez['leftMargin'$pad $offset$this->y + $this->getFontHeight($this->ez['fontSize']$pad $height,$width);
  1387.     }
  1388.  
  1389.     if ($type == "png"){
  1390.         $this->addPngFromFile($image,$this->ez['leftMargin'$pad $offset$this->y + $this->getFontHeight($this->ez['fontSize']$pad $height,$width);
  1391.     }
  1392.     //draw border
  1393.     if ($border != '')
  1394.     {
  1395.     if (!(isset($border['color'])))
  1396.     {
  1397.         $border['color']['red'.5;
  1398.         $border['color']['blue'.5;
  1399.         $border['color']['green'.5;
  1400.     }
  1401.     if (!(isset($border['width']))) $border['width'1;
  1402.     if (!(isset($border['cap']))) $border['cap''round';
  1403.     if (!(isset($border['join']))) $border['join''round';
  1404.  
  1405.  
  1406.     $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
  1407.     $this->setLineStyle($border['width'],$border['cap'],$border['join']);
  1408.     $this->rectangle($this->ez['leftMargin'$pad $offset$this->y + $this->getFontHeight($this->ez['fontSize']$pad $height,$width,$height);
  1409.  
  1410.     }
  1411.     // move y below image
  1412.     $this->y = $this->y - $pad $height;
  1413.     //remove tempfile for remote images
  1414.     if ($temp == trueunlink($image);
  1415.  
  1416. }
  1417. // ------------------------------------------------------------------------------
  1418.  
  1419. // note that templating code is still considered developmental - have not really figured
  1420. // out a good way of doing this yet.
  1421. function loadTemplate($templateFile){
  1422.   // this function will load the requested template ($file includes full or relative pathname)
  1423.   // the code for the template will be modified to make it name safe, and then stored in
  1424.   // an array for later use
  1425.   // The id of the template will be returned for the user to operate on it later
  1426.   if (!file_exists($templateFile)){
  1427.     return -1;
  1428.   }
  1429.  
  1430.   $code implode('',file($templateFile));
  1431.   if (!strlen($code)){
  1432.     return;
  1433.   }
  1434.  
  1435.   $code trim($code);
  1436.   if (substr($code,0,5)=='<?php'){
  1437.     $code substr($code,5);
  1438.   }
  1439.   if (substr($code,-2)=='?>'){
  1440.     $code substr($code,0,strlen($code)-2);
  1441.   }
  1442.   if (isset($this->ez['numTemplates'])){
  1443.     $newNum $this->ez['numTemplates'];
  1444.     $this->ez['numTemplates']++;
  1445.   else {
  1446.     $newNum=0;
  1447.     $this->ez['numTemplates']=1;
  1448.     $this->ez['templates']=array();
  1449.   }
  1450.  
  1451.   $this->ez['templates'][$newNum]['code']=$code;
  1452.  
  1453.   return $newNum;
  1454. }
  1455.  
  1456. // ------------------------------------------------------------------------------
  1457.  
  1458. function execTemplate($id,$data=array(),$options=array()){
  1459.   // execute the given template on the current document.
  1460.   if (!isset($this->ez['templates'][$id])){
  1461.     return;
  1462.   }
  1463.   eval($this->ez['templates'][$id]['code']);
  1464. }
  1465.  
  1466. // ------------------------------------------------------------------------------
  1467. function ilink($info){
  1468.   $this->alink($info,1);
  1469. }
  1470.  
  1471. function alink($info,$internal=0){
  1472.   // a callback function to support the formation of clickable links within the document
  1473.   $lineFactor=0.05// the thickness of the line as a proportion of the height. also the drop of the line.
  1474.   switch($info['status']){
  1475.     case 'start':
  1476.     case 'sol':
  1477.       // the beginning of the link
  1478.       // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
  1479.       if (!isset($this->ez['links'])){
  1480.         $this->ez['links']=array();
  1481.       }
  1482.       $i $info['nCallback'];
  1483.       $this->ez['links'][$iarray('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
  1484.       if ($internal==0){
  1485.         $this->saveState();
  1486.         $this->setColor(0,0,1);
  1487.         $this->setStrokeColor(0,0,1);
  1488.         $thick $info['height']*$lineFactor;
  1489.         $this->setLineStyle($thick);
  1490.       }
  1491.       break;
  1492.     case 'end':
  1493.     case 'eol':
  1494.       // the end of the link
  1495.       // assume that it is the most recent opening which has closed
  1496.       $i $info['nCallback'];
  1497.       $start $this->ez['links'][$i];
  1498.       // add underlining
  1499.       if ($internal){
  1500.         $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1501.       else {
  1502.         $a deg2rad((float)$start['angle']-90.0);
  1503.         $drop $start['height']*$lineFactor*1.5;
  1504.         $dropx cos($a)*$drop;
  1505.         $dropy = -sin($a)*$drop;
  1506.         $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1507.         $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1508.         $this->restoreState();
  1509.       }
  1510.       break;
  1511.   }
  1512. }
  1513.  
  1514. // ------------------------------------------------------------------------------
  1515.  
  1516. function uline($info){
  1517.   // a callback function to support underlining
  1518.   $lineFactor=0.05// the thickness of the line as a proportion of the height. also the drop of the line.
  1519.   switch($info['status']){
  1520.     case 'start':
  1521.     case 'sol':
  1522.  
  1523.       // the beginning of the underline zone
  1524.       if (!isset($this->ez['links'])){
  1525.         $this->ez['links']=array();
  1526.       }
  1527.       $i $info['nCallback'];
  1528.       $this->ez['links'][$iarray('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
  1529.       $this->saveState();
  1530.       $thick $info['height']*$lineFactor;
  1531.       $this->setLineStyle($thick);
  1532.       break;
  1533.     case 'end':
  1534.     case 'eol':
  1535.       // the end of the link
  1536.       // assume that it is the most recent opening which has closed
  1537.       $i $info['nCallback'];
  1538.       $start $this->ez['links'][$i];
  1539.       // add underlining
  1540.       $a deg2rad((float)$start['angle']-90.0);
  1541.       $drop $start['height']*$lineFactor*1.5;
  1542.       $dropx cos($a)*$drop;
  1543.       $dropy = -sin($a)*$drop;
  1544.       $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1545.       $this->restoreState();
  1546.       break;
  1547.   }
  1548. }
  1549.  
  1550. // ------------------------------------------------------------------------------
  1551.  
  1552. }
  1553. ?>

Documentation generated on Mon, 05 May 2008 16:17:08 +0400 by phpDocumentor 1.4.0